layui.use(['jquery', 'element', 'layer', 'form', 'layedit'], function () {
    var $ = layui.$;
    var layedit = layui.layedit;
    var form = layui.form;
    var ctxPath = "/education";


    var initCustomer = function (id) {
        $.ajax({
            url: ctxPath + '/api/customer/listSelectOptions',
            type: 'post',
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if (data != null) {
                        var select = $("#recommenderIds");
                        for (var i = 0; i < data.length; i++) {
                            var sgc = data[i];
                            select.append('<option value="'+sgc.id+'" >'+sgc.name+'</option>');
                        }
                    }
                    form.render();
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });

        if (!id) {
            return;
        }

        $.ajax({
            url: ctxPath + '/api/schoolGradeClass/findById',
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (result) {
                if (result.code == 0) {
                    var data = result.data;
                    if (data != null) {
                        $("input[name='id']").val(data.id);
                        $("input[name='schoolName']").val(data.schoolName);
                        $("input[name='grade']").val(data.grade);
                        $("input[name='clazz']").val(data.clazz);
                        $("#recommenderIds").val(data.recommenderIds);
                    }
                } else {
                    layer.alert("get data fail：" + result.msg);
                }
            }
        });
    }

    initCustomer(window.location.search.substring(4));

    $("#saveBtn").click(function () {
        var request = {
            id: $("input[name='id']").val(),
            schoolName: $("input[name='schoolName']").val(),
            grade: $("input[name='grade']").val(),
            clazz: $("input[name='clazz']").val(),
            recommenderIds:  $("#recommenderIds").val()
        };

        var url = ctxPath + "/api/schoolGradeClass/modify";
        if (!request.id) {
            url = ctxPath + "/api/schoolGradeClass/create";
        }

        $.ajax({
            url: url,
            data: JSON.stringify(request),
            type: 'post',
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if (result.code == 0) {
                    layer.alert('保存成功！', function (index) {
                        layer.close(index);
                        location.href = ctxPath + '/html/schoolGradeClass/schoolGradeClassList.html';
                    });
                } else {
                    layer.alert("保存失败：" + result.msg);
                }
            }
        });
    });

    //按钮事件
    $("#cancelBtn").click(function () {
        location.href = ctxPath + '/html/schoolGradeClass/schoolGradeClassList.html';
    });
});

